package com.fengzheng.basic.common.config.mybatis;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;

//自定义handler处理填充
@Slf4j //日志工具
@Component //必须加 @Component或者@Bean让springboot识别
public class MetaObjectFillHandler implements MetaObjectHandler {

    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        // 起始版本 3.3.3(推荐)
        //自动注入的字段和对应的类型和值
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTimeUtil.of(System.currentTimeMillis(),ZoneId.of("Asia/Shanghai")));
    }

    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");// 起始版本 3.3.3(推荐)
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTimeUtil.of(System.currentTimeMillis(),ZoneId.of("Asia/Shanghai")));
    }
}
